<?php

namespace Controllers\Admin;
use DB\TranslationService;

/**
 * @author Eugene Baranov <evgeny@baranov.ru>
 */
class Translation extends Base {
    /**
     * @var TranslationService
     */
    protected $service;

    public function __construct() {
        parent::__construct();

        $this->service = new TranslationService();
        $this->setView('Translation');
    }

    public function index(array $param) {
        $list = $this->service->getList();
        $result = array(
            'language_list' => array(),
            'translation' => array()
        );
        foreach ($list as $l) {
            $result['translation'][$l['block']][$l['key']][$l['code']] = $l['val'];
            $result['language_list'][$l['code']] = $l['name'];
        }

        return $result;
    }

    public function save(array $param) {
        $this->service->addTranslation($_POST['block'], $_POST['key'], $_POST['value'], $_POST['lang']);
        $this->jsonSuccess(array());
    }

    public function remove(array $param) {
        $this->service->deleteTranslation($param[1], $param[0]);
        $back = isset($_SERVER['HTTP_REFERER']) && ($back = $_SERVER['HTTP_REFERER']) ? $back : '/';
        header('Location: ' . $back);
        exit;
    }
}